package com.definition.init;

import com.definition.bean.IdSequence;
import com.definition.service.IdSequenceService;
import com.definition.service.IdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class IdInit implements ApplicationRunner {
    @Autowired
    private IdSequenceService idSequenceService;
    @Autowired
    private IdService idService;
    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 丢弃上一批次序号，重新开始一个新的序号，在项目启动的时候缓存。
        IdSequence idSequence = idSequenceService.lambdaQuery().eq(IdSequence::getServiceName, "orderService").one();
        if (idSequence==null){
            throw new RuntimeException("未初始化数据库");
        }
        idService.load("orderService");
    }
}
